﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Common.Session;
using Core.Message.Entities;
using Core.ExceptionHandling;
using Core.Controls.Message;
using System.Data;
using vHCS.SYS.Business;
using Telerik.Web.UI;
using vHCS.HR.Business;
using vHCS.HR.Entities;

namespace vHCS.Web.MdlHR.Controls
{
    /// <summary>
    /// Thông tin nhân viên
    /// </summary>
    public partial class TER_TerminationSelect : Core.NTier.UserInterface.BaseUserControl
    {
        #region Public Properties
        /// <summary>
        /// Username của nhân viên cần hiển thị thông tin
        /// </summary>
        public string Username
        {
            set { base.SetComboValue(this.cboUsername, value); }
            get { return this.cboUsername.SelectedValue.Trim(); }
        }
        /// <summary>
        /// Mã nhân viên
        /// </summary>
        public long EmployeeId
        {
            get
            {
                long lEmpId = 0;
                long.TryParse(hdfEmployeeId.Value, out lEmpId);
                return lEmpId;
            }
        }
        public string Reportingline
        {
            get
            {
                return hdfReportingline.Value;
            }
        }
        /// <summary>
        ///Tự động load thông tin nhân viên theo tên đăng nhập hiện tại
        /// </summary>
        public bool AutoLoad
        {
            set { this.hdfAutoLoad.Value = value.ToString(); }
            get
            {
                bool autoload = false;
                bool.TryParse(this.hdfAutoLoad.Value.Trim(), out autoload);
                return autoload;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadDefaultUser();
            }

        }

        /// <summary>
        /// Hiển thị thông tin user mặc định
        /// </summary>
        private void LoadDefaultUser()
        {
            try
            {
                if (this.AutoLoad)
                {
                    string username = Username;
                    if (string.IsNullOrEmpty(username)) username = SessionManager.Username;
                    this.LoadEmpInfo(username);
                }
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Hiển thị thông tin nhân viên
        /// </summary>
        public void LoadEmpInfo(string username)
        {
            try
            {
                HR_EmpCVCollection controller = new HR_EmpCVCollection();
                long empId = controller.GetEmpIdByUsername(username);
                this.LoadEmpInfoByEmpId(empId);

            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        public void LoadEmpInfoByEmpId(long empId)
        {
            try
            {
                this.ClearForm();
                HR_EmpCVCollection controller = new HR_EmpCVCollection();
                HR_EmpHeaderDTO empInfo = controller.GetEmpHeader(empId);
                if (empInfo != null)
                {
                    this.txtEmpCode.Text = empInfo.EmpCode;
                    this.txtFullName.Text = empInfo.FullName;
                    this.txtBarcode.Text = empInfo.Barcode;
                    this.txtOrgChart.Text = empInfo.OrgChart;
                    this.txtMgtJob.Text = empInfo.MgtJob;
                    this.txtProJob.Text = empInfo.ProJob;
                    this.txtEmpType.Text = empInfo.EmpType;
                    this.txtContractType.Text = empInfo.ContactType;
                    this.txtWorkingOffice.Text = empInfo.WorkingOffice;
                    this.txtJobTitle.Text = empInfo.JobTitle;
                    this.txtJobLevel.Text = empInfo.JobLevel;
                    this.txtReportTo.Text = empInfo.ReportTo;
                    if (empInfo.JoinDate.Year > 1)
                        this.txtJoinDate.Text = empInfo.JoinDate.ToShortDateString();

                    this.rbtActive.SelectedValue = empInfo.Active.ToString();
                    this.hdfEmployeeId.Value = empInfo.EmployeeId.ToString();
                    this.hdfReportingline.Value = empInfo.ReportTo;
                    base.SetComboValue(this.cboUsername, empInfo.UserName);
                }
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Sự kiện thay đổi tên đăng nhập
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cboUsername_SelectedIndexChanged(object sender,
            Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.LoadEmpInfo(this.cboUsername.SelectedValue);
            if (this.UserChanged != null)
                this.EmployeeChanged(sender, null);
        }


        /// <summary>
        /// Sau khi thay đổi nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EmployeeChanged(object sender, EventArgs e)
        {
            try
            {
                string strUsername = string.Empty;
                //Gọi từ hàm thay đổi giá trị của cboUsername
                if (sender is RadComboBox)
                {
                    strUsername = this.cboUsername.SelectedValue.Trim();
                    this.LoadEmpInfo(strUsername);
                }
                else
                {
                    long empId = 0;
                    long.TryParse(hdfEmployeeId.Value.Trim(), out empId);
                    this.LoadEmpInfoByEmpId(empId);
                }
                //Raise sự kiện thay đổi nhân viên
                if (this.UserChanged != null)
                    this.UserChanged(this, e);
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Xóa thông tin của form
        /// </summary>
        protected void ClearForm()
        {
            this.txtEmpCode.Text = null;
            this.txtFullName.Text = null;
            this.txtBarcode.Text = null;
            this.txtOrgChart.Text = null;
            this.txtMgtJob.Text = null;
            this.txtProJob.Text = null;
            this.txtEmpType.Text = null;
            this.txtContractType.Text = null;
            this.txtWorkingOffice.Text = null;
            this.txtJobTitle.Text = null;
            this.txtJobLevel.Text = null;
            this.txtReportTo.Text = null;
            this.txtJoinDate.Text = null;
            this.rbtActive.SelectedValue = null;
            this.hdfEmployeeId.Value = null;
            base.SetComboValue(this.cboUsername, null);
        }

        #region Custom Event
        /// <summary>
        /// Raise sự kiện thay đổi nhân viên
        /// </summary>
        public event System.EventHandler UserChanged;
        #endregion
    }
}